﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using UnityEngine.UI;
using Random = System.Random;

/**
 *     功能:
 *         1.
 *
 *
 *     说明:
 *         1.本类是单例模式调用,需挂载到游戏物体才可使用
 *
 * 
 *     创建时间:2019-7-6 14:25:19
 *     修改时间:2019-7-6 14:25:22
 *         
 */

public class BooldControl : MonoBehaviour
{
    //简单单例///////////////////////////////////
    public static BooldControl Instance;

    private void Awake()
    {
        Instance = this;
    }
    //////////////////////////////////////////////


    public float m_maxHealth; //最大健康值

    public float m_maxMood; //最大心情值

    public float m_health; //当前健康值
    public float m_mood; //当前心情值

    //健康值
    public Scrollbar m_healthBar;

    //心情值
    public Scrollbar m_moodBar;

    //小人表情包图片
    public Sprite[] m_people;

    //表情包位置
    public Image m_hahaha;

    public float Health
    {
        get { return m_health; }
        set
        {
            m_health = value;
            m_health = m_health > m_maxHealth ? m_maxHealth : value;
            Decide();
        }
    }

    public float Mood
    {
        get { return m_mood; }
        set
        {
            m_mood = value;
            m_mood = m_mood > m_maxMood ? m_maxMood : value;
            Decide();
        }
    }

    private void Start()
    {
        SetHealth();
        SetMood();
    }

    //改变健康值
    private void SetHealth()
    {
        m_healthBar.size = m_health / m_maxHealth * 1f;
        m_healthBar.transform.Find("SlidingArea/Handle/Text").GetComponent<Text>().text = "健康值:" + m_health;
    }

    //改变心情值
    private void SetMood()
    {
        m_moodBar.size = m_mood / m_maxMood * 1f;
        m_moodBar.transform.Find("SlidingArea/Handle/Text").GetComponent<Text>().text = "心情值:" + m_mood;
    }

    //判定血条条件
    private void Decide()
    {
        //改变健康值
        SetHealth();
        //改变心情值
        SetMood();
        if (m_health >= m_maxHealth && m_mood >= m_maxMood) //判断心情值与健康值都满,游戏胜利
        {
            CallState_0();
        }
        else if (m_health <= 0 && m_mood <= 0) //健康值与心情值都到底了
        {
            CallState_1();
        }
        else if (m_health >= m_maxHealth && m_mood < m_maxMood) //其中一个值满了,另一个值没满 (生命值满)
        {
            CallState_2(true, false);
        }
        else if (m_mood >= m_maxMood && m_health < m_maxHealth) //其中一个值满了,另一个值没满 (心情值满)
        {
            CallState_2(false, true);
        }
        else if (m_health <= 0 || m_mood <= 0) //当其中一个值0时候
        {
            CallState_3();
        }

        ToggleSprite(0, 0);
    }

    //心情值与健康值都满,游戏胜利
    private void CallState_0()
    {
        print("心情值与健康值都满,游戏胜利");
        Hint.Instance.ShowHint("游戏胜利, 你真厉害!(σﾟ∀ﾟ)σ..:*☆哎哟不错哦");
    }

    //健康值与心情值都到底了
    private void CallState_1()
    {
        print("健康值与心情值都到底了");
        Hint.Instance.ShowHint("健康值心情值都没了,人生已经失去意义了ლ(´ڡ`ლ)好吃的.");
    }

    //其中一个值满了,另一个值没满
    private void CallState_2(bool health, bool mood)
    {
        if (health)
        {
            print("其中一个值满了,另一个值没满(健康满)");
            //Hint.Instance.ShowHint("加油!!!还差一点就赢了 (^_−)☆");
        }
        else if (mood)
        {
            print("其中一个值满了,另一个值没满(心情满)");
            //Hint.Instance.ShowHint("加油!!!还差一点就赢了 Thanks♪(･ω･)ﾉ");
        }
    }

    //当其中一个值0时候
    private void CallState_3()
    {
        print("游戏失败");
        Hint.Instance.ShowHint("游戏失败Over Σσ(・Д・；)我我我什么都没做!!!");
    }


    //==================================
    //切换图片
    public void ToggleSprite(float health, float mood)
    {
        m_hahaha.sprite = m_people[UnityEngine.Random.Range(0, 5)];
    }


    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.J))
        {
            Health += 10;
        }
        else if (Input.GetKeyDown(KeyCode.K))
        {
            Mood += 10;
        }
    }
}